package com.zhengsr.colorselector;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by zhengshaorui on 2018/1/23.
 */

public class ColorView extends View implements View.OnTouchListener {

    private static final String TAG = "ColorView";
    private Bitmap mBitmap;
    private Rect mSrcRect;
    private Rect mDesRect;
    private Paint mPaint;
    private int mWidth,mHeigth;
    private Paint mSPaint;
    private float mPointx ,mPointy ;

    public ColorView(Context context) {
        this(context,null);
    }

    public ColorView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.circle);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
        mSrcRect = new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight());

        mSPaint = new Paint();
        mSPaint.setAntiAlias(true);
        mSPaint.setFilterBitmap(true);
        mSPaint.setStyle(Paint.Style.STROKE);
        mSPaint.setStrokeWidth(2);
        mSPaint.setColor(Color.GRAY);

        setClickable(true);
        setOnTouchListener(this);
    }

    public interface ColorListener{
        void getColor(int color);
    }
    private ColorListener mColorListener;
    public void setColorListener(ColorListener listener){
        mColorListener = listener;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        float x = mPointx =  event.getX();
        float y = mPointy =  event.getY();
        boolean isinCircle = isInCircle(x,y);
        if (!isinCircle){
            return false;
        }
        invalidate();

        int dx = (int) ( (x / getWidth()) * mBitmap.getWidth());
        int dy = (int) ( (y / getHeight())  * mBitmap.getHeight());

        int color = mBitmap.getPixel(dx,dy);
        if (mColorListener != null){
            mColorListener.getColor(color);
        }
        return true;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mBitmap,mSrcRect,mDesRect,mPaint);

        canvas.drawCircle(mPointx,mPointy,4,mSPaint);

    }


    private boolean isInCircle(float x,float y){
        int radius = mWidth/2;
        float cx = mWidth/2;
        float cy = mHeigth/2;
        double d = (cx - x) * (cx - x) + (cy - y) * (cy - y);
        return radius >= Math.abs(Math.sqrt(d));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        mWidth = measureWidth(widthMeasureSpec);
        mHeigth = measureHeight(heightMeasureSpec);
        mPointx = mWidth/2;
        mPointy = mHeigth/2;
        mDesRect = new Rect(0,0,mWidth,mHeigth);
        setMeasuredDimension(mWidth,mHeigth);
    }

    //设置高的大小
    private int measureHeight(int heightMeasureSpec) {
        // TODO Auto-generated method stub
        int result = 0;
        //获取模式和大小
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }else{
            result = 70; //如果是wrap_content ,给个初始值
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
    //设置宽的大小
    private int measureWidth(int widthMeasureSpec) {
        // TODO Auto-generated method stub
        int result = 0;
        //获取模式和大小
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }else{
            result = 70; //如果是wrap_content ,给个初始值
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
}
